package com.coolbi.onsupai.core.biz.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.onsupai.core.biz.OrderBiz;
import com.coolbi.onsupai.core.constant.CouponConstant;
import com.coolbi.onsupai.core.dao.CouponDao;
import com.coolbi.onsupai.core.dao.CustomerDao;
import com.coolbi.onsupai.core.dao.GoodsInfoDao;
import com.coolbi.onsupai.core.dao.OrderDao;
import com.coolbi.onsupai.core.entity.Coupon;
import com.coolbi.onsupai.core.entity.Order;
import com.coolbi.onsupai.core.entity.OrderItem;
import com.coolbi.onsupai.core.exception.ServiceException;
import com.coolbi.onsupai.core.util.CouponUtils;
import com.coolbi.onsupai.core.util.GoodsUtils;
import com.coolbi.onsupai.core.util.OrderUtils;

public class OrderBizImpl implements OrderBiz {

	@Autowired
	private OrderDao orderDao;
	@Autowired
	private CouponDao couponDao;
	@Autowired
	private CustomerDao customerDao;
	@Autowired
	private GoodsInfoDao goodsInfoDao;
	
	@Override
	public Order save(Order obj) {
		try {
			orderDao.save(obj);
		} catch (Exception e) {
			throw new ServiceException("添加订单发生错误！", e);
		}
		return obj;
	}

	@Override
	public void delete(long id) {
		try {
			orderDao.delete(id);
		} catch (Exception e) {
			throw new ServiceException("删除订单发生错误！", e);
		}
	}

	@Override
	public void update(Order obj) {
		try {
			orderDao.update(obj);
		} catch (Exception e) {
			throw new ServiceException("修改订单发生错误！", e);
		}
	}

	@Override
	public Order get(long id) {
		return orderDao.get(id);
	}

	@Override
	public List<Order> list() {
		return orderDao.list();
	}

	@Override
	public List<Order> listByPage(int offset, int limit) {
		return orderDao.listByPage(offset, limit);
	}

	@Override
	public List<Order> find(Map<String, Object> parameter) {
		return orderDao.find(parameter);
	}

	@Override
	public List<Order> findByPage(Map<String, Object> parameter, int offset,
			int limit) {
		return orderDao.findByPage(parameter, offset, limit);
	}

	@Override
	public List<Order> search(Map<String, Object> parameter, int offset,
			int limit) {
		return orderDao.search(parameter, offset, limit);
	}

	@Override
	public int count() {
		return orderDao.count();
	}

	@Override
	public int countByKeyWord(Map<String, Object> parameter) {
		return orderDao.countByKeyWord(parameter);
	}

	@Override
	public int count(Map<String, Object> parameter) {
		return orderDao.count(parameter);
	}

	@Override
	public Order saveOrder(Order order, OrderItem orderItem, Long userId) {
		// 1、优惠券使用
		double equivalence = 0d;
		if (null != orderItem.getCouponNo() && !orderItem.getCouponNo().trim().equals("")) {
			Coupon coupon = couponDao.findByCouponNo(orderItem.getCouponNo(), order.getCustomerId(), CouponConstant.UNUSE);
			// 验证优惠券是否可用
			CouponUtils.validateCoupon(coupon);
			equivalence = coupon.getEquivalence();
			// 修改优惠券使用状态
			if (couponDao.updateCouponStatus(orderItem.getCouponNo(), order.getCustomerId(), CouponConstant.USED) < 0) {
				throw new ServiceException("修改优惠券使用状态失败");
			}
		}
		// 2、客户信息修改
		if (customerDao.updateTrade(order.getCustomerId(), orderItem.getCreditNum()) < 0) {
			throw new ServiceException("修改客户信息失败");
		}
		// 3、商品信息修改
		if (goodsInfoDao.updateByBatch(GoodsUtils.encapsulList(order.getGoodsIds(), orderItem.getPrices(), orderItem.getStocks())) < 0) {
			throw new ServiceException("批量修改商品最近售价失败");
		}
		// 4、发票信息生成
		
		// 5、订单信息生成
		double amount = OrderUtils.payAmount(orderItem.getPrices(), order);
		double payMoney = amount + order.getPostAge();
		if (payMoney > equivalence) {
			payMoney -= equivalence;
		} else {
			payMoney = 0;
		}
		order.setTotleSum(amount);
		order.setPayMoney(payMoney);
		if (null == order.getCreateDate()) {
			order.setCreateDate(new Date());
		}
		// 6、订单编号生成（商家编号0A+当前时间+今日订单序号）
		order.setOrderNo(OrderUtils.generateOrderNo());
		// 7、订单生成
		order = orderDao.save(order);
		if (null == order.getId()) {
			throw new ServiceException("订单生成失败");
		}
		return order;
	}

	@Override
	public void confirmOrderConsign(Long id, int orderStatus) {
		try {
			if (orderDao.confirmOrderConsign(id, orderStatus) < 0) {
				throw new ServiceException("确认订单发货发生失败！");
			}
		} catch (Exception e) {
			throw new ServiceException("确认订单发货发生错误！", e);
		}
	}
	
	@Override
	public void modifyOrderStatus(Long id, int orderStatus) {
		try {
			if (orderDao.confirmOrderConsign(id, orderStatus) < 0) {
				throw new ServiceException("修改订单发生失败！");
			}
		} catch (Exception e) {
			throw new ServiceException("修改订单发生错误！", e);
		}
	}
	
	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public void setCouponDao(CouponDao couponDao) {
		this.couponDao = couponDao;
	}


	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}

	public void setGoodsInfoDao(GoodsInfoDao goodsInfoDao) {
		this.goodsInfoDao = goodsInfoDao;
	}

	@Override
	public List<Order> findByIds(List<Long> orderIds) {
		return orderDao.findByIds(orderIds);
	}
}
